package o;

import android.app.Activity;
import com.huawei.appgallery.foundation.card.base.bean.KeywordInfo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;

public class cvk extends BaseSharedPref {
    private static final Object ˎ = new Object();
    private static volatile cvk ॱ = null;

    private cvk() {
    }

    public static synchronized cvk ॱ() {
        cvk o_cvk;
        synchronized (cvk.class) {
            synchronized (ˎ) {
                if (ॱ == null) {
                    ॱ = new cvk();
                }
                o_cvk = ॱ;
            }
        }
        return o_cvk;
    }

    public List<KeywordInfo> ˎ(Activity activity) {
        int ˎ = dgm.ˎ(activity);
        if (ˎ != 0 && ˎ != 5 && ˎ != 30) {
            return Collections.emptyList();
        }
        String ॱ = ॱ.ॱ("Search_serviceType_" + ˎ, "");
        if (dof.ॱ(ॱ)) {
            return Collections.emptyList();
        }
        try {
            JSONArray jSONArray = new JSONArray(ॱ);
            List<KeywordInfo> arrayList = new ArrayList(jSONArray.length());
            int length = jSONArray.length();
            for (int i = 0; i < length; i++) {
                KeywordInfo keywordInfo = new KeywordInfo();
                keywordInfo.ˎ(jSONArray.getString((length - i) - 1));
                arrayList.add(keywordInfo);
            }
            return arrayList;
        } catch (JSONException e) {
            cux.ˏ.ˊ("SearchHistorySharedPreference", "readSearchHistory(activity) " + e.getMessage());
            return Collections.emptyList();
        }
    }

    public void ˊ(Activity activity) {
        int ˎ = dgm.ˎ(activity);
        if (ˎ == 0 || ˎ == 5 || ˎ == 30) {
            ॱ.ˏˏ();
        }
    }

    private void ˎ(String str, String str2, int i) {
        try {
            JSONArray jSONArray;
            if (dof.ॱ(str2)) {
                jSONArray = new JSONArray();
            } else {
                jSONArray = new JSONArray(str2);
                for (int i2 = 0; i2 < jSONArray.length(); i2++) {
                    if (dof.ˎ(str, jSONArray.getString(i2))) {
                        jSONArray.remove(i2);
                        break;
                    }
                }
            }
            jSONArray.put(str);
            if (jSONArray.length() > 10) {
                jSONArray.remove(0);
            }
            ॱ.ˋ("Search_serviceType_" + i, jSONArray.toString());
        } catch (JSONException e) {
            cux.ˏ.ˊ("SearchHistorySharedPreference", "saveHistory() " + e.getMessage());
        }
    }

    public void ˏ(Activity activity, String str) {
        int ˎ = dgm.ˎ(activity);
        if (ˎ == 0 || ˎ == 5 || ˎ == 30) {
            ˎ(str, ॱ.ॱ("Search_serviceType_" + ˎ, ""), ˎ);
        }
    }
}
